<?php

class MyHelpers {
    protected static $instance = null;
    
    private function __construct() {}
    
    public static function getInstance() {
        if (!isset(static::$instance)) {
            static::$instance = new static;
        }
        return static::$instance;
    }
    
    public function testVar($var, $exits = false) {
        echo '<pre>';
        print_r($var);
        echo '</pre>';
        if ($exits) Yii::app()->end();

        return true;
    }

    /**
     * Generate random key with length in 8 character
     * @return string
     */
    public function generateKey() {
        $length = 8;
        $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charsLength = strlen($chars) - 1;
        $randomString = '';
        
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $chars[rand(0, $charsLength)];
        }
        
        return $randomString;
    }
    
    public function log($data, $fileLog = false) {
        if ( $fileLog ) {
            $logPath = Yii::getPathOfAlias('application.runtime') . '/test_var.html';
            if ( is_file($logPath) ) unlink($logPath);
            Yii::log(CVarDumper::dumpAsString($data, 10, true), 'info', 'test-var-1');
        } else {
            Yii::log(CVarDumper::dumpAsString($data, 10, false), 'info', 'test-var-2');
        }
        
    }

}